var FriendsView = function(){};

FriendsView.userInfo = null;
FriendsView.backendURL = null;
FriendsView.debug = true;
FriendsView.PAGE_SIZE = 200;
FriendsView.FRIENDS_COUNT = 3;

FriendsView.selectedFriendItems;


FriendsView.embed = function(container)
{
	FriendsView.selectedFriends = [];
	FriendsView.selectedFriendItems = [];
	
	var friendsView = new FriendsView();
	friendsView.initialize(document.getElementById(container));
}

FriendsView.prototype.loading = false;
FriendsView.prototype.container = null;
FriendsView.prototype.currentPage = 0;
FriendsView.prototype.friends = null;

FriendsView.prototype.loadingIndicate = null;

FriendsView.prototype.initialize = function(container)
{
	this.container = container;
	this.container.style.width = "354px";
	this.container.style.position = "relative";
	var self = this;
	
	/**for test**/
	if(parent == null || parent.JielongCNY == null)
	{
		if(FriendsView.debug)
		{
			FriendsView.userInfo = {name:"七宝",friends_count:33};
			FriendsView.backendURL = "http://jielong.local.com/api/";
		}else{
			alert("此页面现在不可用。");
			return;
		}
	}else{
		FriendsView.userInfo = parent.JielongCNY.jielongApp.userInfo;
		FriendsView.backendURL = parent.JielongCNY.backendURL;
	}
		
	if(FriendsView.userInfo == null)
	{
		alert("请先登录！！！");
		return;
	}
	
	this.currentPage = 1;
	this.friends = [];
	$.get(FriendsView.backendURL + "get_friends_list.php",
	      {page_size:FriendsView.PAGE_SIZE,page:this.currentPage},
		  function(data){self.onDataInit(data);},
		  "json");
}

FriendsView.prototype.onDataInit = function(data)
{
	if(data.error == null)
	{
		if(data.length <= 0)
		{
			alert("你还没有任何好友，请先去微博添加好友！");
			return;
		}
		
		this.renderFriends(data);
		this.friends = this.friends.concat(data);
		var self = this;
		window.onscroll = function()
		{
			if(window.scrollY >= document.documentElement.scrollHeight - document.documentElement.clientHeight &&
			   !self.loading)
				self.loadNextPage();
		};
	}else{
		alert("现在无法连接到微博，请稍后再试！");
	}
}

FriendsView.prototype.loadNextPage = function()
{
	if(this.friends.length >= FriendsView.userInfo["friends_count"])
		return;
	
	if(this.loadingIndicate == null)
	{
		var loadingContainer = document.createElement("div");
		loadingContainer.style.position = "absolute";
		loadingContainer.style.width = FriendItemView.WIDTH * 2 + FriendItemView.SPACE + "px";
		loadingContainer.style.height = FriendItemView.HEIGHT + "px";
		loadingContainer.style.color = "#FFFFFF";
		loadingContainer.innerHTML = "loading...";
		loadingContainer.style.textAlign = "center";
		
		this.loadingIndicate = loadingContainer;
	}
	
	this.loadingIndicate.style.left = "0px";
	this.loadingIndicate.style.top = Math.ceil(this.friends.length / 2) * FriendItemView.HEIGHT + "px";
	this.container.appendChild(this.loadingIndicate);
	this.container.style.height = (Math.ceil(this.friends.length / 2) + 1) * FriendItemView.HEIGHT + "px";
	this.loading = true;
	
	var self = this;
	this.currentPage ++;
	$.get(FriendsView.backendURL + "get_friends_list.php",
	      {page_size:FriendsView.PAGE_SIZE,page:this.currentPage},
		  function(data){self.onFriendDataLoaded(data);},
		  "json");
}

FriendsView.prototype.onFriendDataLoaded = function(data)
{
	this.container.removeChild(this.loadingIndicate);
	this.container.style.height = Math.ceil(this.friends.length / 2) * FriendItemView.HEIGHT + "px";
	this.loading = false;
	
	if(data.error == null)
	{
		this.renderFriends(data);
		this.friends = this.friends.concat(data);
	}else{
		alert("现在无法连接到微博，请稍后再试！");
	}
}

FriendsView.prototype.renderFriends = function(data)
{
	for(var i = 0;i < data.length;i++)
	{
		var item = new FriendItemView(data[i]);
		var x = (this.friends.length + i) % 2 * (FriendItemView.SPACE + FriendItemView.WIDTH);
		var y = Math.floor((this.friends.length + i) / 2) * FriendItemView.HEIGHT;
		item.moveTo(x,y);
		this.container.appendChild(item.view);
		this.container.style.height = y + FriendItemView.HEIGHT + "px";
	}
}


var FriendItemView = function(friend)
{
	this.friend = friend;
	var self = this;
	
	var view = document.createElement("div");
	this.view = view;
	view.style.position = "absolute";
	view.style.width =  FriendItemView.WIDTH + "px";
	view.style.height = FriendItemView.HEIGHT + "px";
	view.style.background = "#000000"
	
	var container = document.createElement("div");
	container.style.position = "relative";
	container.display = "block";
	container.style.width =  FriendItemView.WIDTH + "px";
	container.style.height = FriendItemView.HEIGHT + "px";
	view.appendChild(container);
	
	var upperBorder = document.createElement("img");
	upperBorder.src = "images/friend_item_border.jpg";
	upperBorder.width = 162;
	upperBorder.height = 2;
	upperBorder.style.position = "absolute";
	upperBorder.style.left = "0px";
	upperBorder.style.top = "0px";
	container.appendChild(upperBorder);
	
	var btnSelected = document.createElement("img");
	this.btnSelected = btnSelected;
	btnSelected.style.border = "0";
	btnSelected.style.width = "14px";
	btnSelected.style.height = "17px";
	btnSelected.style.position = "absolute";
	btnSelected.style.left = "1px";
	btnSelected.style.top = "9px";
	btnSelected.width = 14;
	btnSelected.height = 17;
	btnSelected.src = "images/unselected_icon.jpg";
	container.appendChild(btnSelected);
	
	var span = document.createElement("span");
	var count = 0;
	var showName = friend["name"];
	for(var i = 0;i < friend["name"].length;i++)
	{
		var pattern = /\w/;
		if(pattern.test(friend["name"].charAt(i)))
			count += 1;
		else
			count += 2;
		
		if(count >= 18 && i + 1 < friend["name"].length)
			showName = showName.slice(0,i+1) + "...";
	}
	span.innerHTML = showName;
	span.style.position = "absolute";
	span.style.left = "25px";
	span.style.top = "11px";
	span.style.fontFamily = "'STSong','宋体','STKaiti','楷体','STHeiti','黑体'";
	span.style.color = "#FFFFFF";
	span.style.fontSize = "12px";
	span.style.letterSpacing = "1px";
	container.appendChild(span);
	
	container.onclick = function()
	{
		self.selected = !self.selected;
		btnSelected.src = self.selected ? "images/selected_icon.jpg" : "images/unselected_icon.jpg";
		
		if(self.selected)
		{
			FriendsView.selectedFriendItems.push(self);
			if(FriendsView.selectedFriendItems.length > FriendsView.FRIENDS_COUNT)
			{
				var delFriend = FriendsView.selectedFriendItems.shift();
				delFriend.setSelected(false);
			}
		}else{
			FriendsView.selectedFriendItems.splice(FriendsView.selectedFriendItems.indexOf(self),1);
		}
	};
}

FriendItemView.HEIGHT = 32;
FriendItemView.WIDTH = 162;
FriendItemView.SPACE = 31;

FriendItemView.prototype.view = null;
FriendItemView.prototype.friend = null;
FriendItemView.prototype.selected = false;
FriendItemView.prototype.btnSelected = null;

FriendItemView.prototype.setSelected = function(selected)
{
	this.selected = selected;
	this.btnSelected.src = this.selected ? "images/selected_icon.jpg" : "images/unselected_icon.jpg";
}

FriendItemView.prototype.moveTo = function(x,y)
{
	this.view.style.left = x + "px";
	this.view.style.top = y + "px";
}
